package com.ebs.md.fxmonitor.marketmonitor;

/**
 * Perforce Revision:
 *  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/md/fxmonitor/marketmonitor/AbstractMonitorListener.java $
 *  $Revision: #6 $  $Change: 114794 $  $DateTime: 2012/09/10 13:50:01 $  $Author: Arik.Sher $
 */

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.apache.log4j.Logger;

import com.ebs.commons.comp.IParsedPayload;


public abstract class AbstractMonitorListener
implements MessageListener
          ,MarketMonitorConsts
{
    final protected Logger logger = Logger.getLogger(getClass());
    final protected String shortClassName; // used mainly for logging

    protected boolean useMarketMonitor;

    public AbstractMonitorListener ()
    {
    	useMarketMonitor = true;
		shortClassName = getClass().getSimpleName();
    }
	
    public void onMessage(Message message) {
		//if useMarketMonitor flag is set to false then we are not using market inspector.
		if ( useMarketMonitor == false ){
			return;
		}
		
		IParsedPayload payload = extractPayload(message);
        
        if ( payload != null ) {
        	handleParsedPayload(payload);
        }
	}
	
    protected IParsedPayload extractPayload(Message message) {
    	if ( message == null ) {
    		logger.error("incoming JMS Message is null!");
    	}
        IParsedPayload payload = null;

        if(message instanceof ObjectMessage) {
            ObjectMessage   objMsg  = (ObjectMessage) message;
            try {
				payload = (IParsedPayload) objMsg.getObject();
			} catch (JMSException e) {
				logger.error("failed extracting ParsedPayload from provided JMS message.", e);
			}
        } else {
        	logger.warn("expected JMS Message is not Object one. toString of it: '" + message.toString() + "'");
        }

        return payload;
    }
    
    protected abstract void handleParsedPayload(IParsedPayload payload);
    
    public void setUseMarketMonitor(boolean useMarketMonitor){
    	this.useMarketMonitor = useMarketMonitor;
    }

}
